package com.hhu.softpractise.service.impl;
import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhu.softpractise.model.dto.postcomment.PostCommentAddRequest;
import com.hhu.softpractise.model.dto.postcomment.PostCommentQueryRequest;
import com.hhu.softpractise.model.entiy.PostComment;
import com.hhu.softpractise.service.PostCommentService;
import com.hhu.softpractise.mapper.PostCommentMapper;
import com.hhu.softpractise.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* @author 35534
* @description 针对表【post_comment(帖子评论)】的数据库操作Service实现
* @createDate 2025-06-23 14:16:35
*/
@Service
public class PostCommentServiceImpl extends ServiceImpl<PostCommentMapper, PostComment>
    implements PostCommentService{

    @Resource
    private UserService userService;

    @Override
    public Long addPostComment(PostCommentAddRequest postCommentAddRequest, HttpServletRequest request) {
        Long userId = userService.getLoginUser(request).getId();
        PostComment postComment = new PostComment();
        BeanUtils.copyProperties(postCommentAddRequest, postComment);
        postComment.setUserId(userId);
        boolean save = saveOrUpdate(postComment);
        return save ? postComment.getId() : -1;
    }

    @Override
    public List<PostComment> getlist(PostCommentQueryRequest postCommentQueryRequest) {
        Long postId = postCommentQueryRequest.getPostId();
        Date createTime = postCommentQueryRequest.getCreateTime();
        int current = postCommentQueryRequest.getCurrent();
        int pageSize = postCommentQueryRequest.getPageSize();
        if(pageSize == 0){
            pageSize = 10;
        }
        String sortField = postCommentQueryRequest.getSortField();
        String sortOrder = postCommentQueryRequest.getSortOrder();

        QueryWrapper<PostComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(postId != null, "postId", postId);
        queryWrapper.eq(createTime != null, "create_time", createTime);
        queryWrapper.orderBy(sortField != null, sortOrder.equals("asc"), sortField);
        Page<PostComment> page = new Page<>(current, pageSize);
        return page(page, queryWrapper).getRecords();
    }
}




